import axios from 'axios';

interface Post {
  userId: number;
  id:      number;
  title:   string;
  body:    string;
}

async function render() {
  try {
    const { data } = await axios.get<Post[]>(
      'https://jsonplaceholder.typicode.com/posts?_limit=3'
      //免费的在线 REST API 测试接口，由 JSONPlaceholder 提供
    );
    const ul = document.createElement('ul');
    data.forEach(p => {
      const li = document.createElement('li');
      li.textContent = `#${p.id} ${p.title}`;
      ul.appendChild(li);
    });
    document.body.appendChild(ul);
  } catch (e) {
    document.body.textContent = '请求失败：' + (e as Error).message;
  }
}

document.addEventListener('DOMContentLoaded', render);